﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using GraphGeneric;
using System.Windows.Shapes;
using System.Windows;
using System.Windows.Media;

namespace GraphGenericTestApp
{
	public class NodeCanvas : Canvas
	{
		public NodeCanvas(Node node, GraphCanvas parentCanvas)
		{
			
			m_node = node;
			m_parentCanvas = parentCanvas;

			MinWidth = 100;
			MinHeight = 50;
			Width = MinWidth;
			Height = MinHeight;
			m_shape = new Ellipse();
			m_shape.Fill = Brushes.DarkSlateBlue;
			m_shape.Stroke = Brushes.CornflowerBlue;
			m_shape.StrokeThickness = 2;
            m_shape.Opacity = .5;

			m_textBlock = new TextBlock();
			m_textBlock.Text = m_node.Value;
			m_textBlock.FontFamily = new FontFamily("Ariel");
            m_textBlock.Foreground = Brushes.CornflowerBlue;
            m_textBlock.FontSize = 24;
			Children.Add(m_shape);
			Children.Add(m_textBlock);
		}


		public Point Location
		{
			get { return (Point)GetValue(LocationProperty); }
			set { SetValue(LocationProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Location.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty LocationProperty =
			 DependencyProperty.Register("Location", typeof(Point), typeof(NodeCanvas), 
			 new UIPropertyMetadata(new Point(0,0), new PropertyChangedCallback(OnLocationChanged)));

		private static void OnLocationChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
		{
			NodeCanvas node = sender as NodeCanvas;
			Canvas.SetLeft(node, node.Location.X);
			Canvas.SetTop(node, node.Location.Y);
		}

		protected override void OnInitialized(EventArgs e)
		{
			m_textBlock.SizeChanged += new SizeChangedEventHandler(m_textBlock_SizeChanged);
			base.OnInitialized(e);
		}

		void m_textBlock_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
		{
			Width = e.NewSize.Width + 30;
			Height = e.NewSize.Height + 30;
			m_shape.Width = Width < MinWidth ? MinWidth : Width;
			m_shape.Height = Height < MinHeight ? MinHeight : Height;
			m_boundingBox.Width = m_shape.Width;
			m_boundingBox.Height = m_boundingBox.Height;
			Canvas.SetLeft(m_textBlock, m_shape.Width / 2 - e.NewSize.Width / 2);
			Canvas.SetTop(m_textBlock, m_shape.Height / 2 - e.NewSize.Height / 2);
		}

		private Node m_node;
		private GraphCanvas m_parentCanvas;

		private Rect m_boundingBox = new Rect();
		private Shape m_shape = new Ellipse();
		private TextBlock m_textBlock = new TextBlock();

	}
}
